package com.itheima.leyou.queue;

import com.itheima.leyou.dao.OrderDao;
import com.itheima.leyou.service.OrderService;
import com.itheima.leyou.util.StringUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

@Component
public class OrderQueue {

    private static final String ORDER_QUEUE = "order_queue";
    private static final String ORDER_STATUS_QUEUE = "order_status_queue";

    @Resource
    private OrderService orderService;

    @Resource
    private OrderDao orderDao;

    private Logger logger = Logger.getLogger(String.valueOf(OrderQueue.class));

    @RabbitListener(queues = ORDER_QUEUE)
    public void getOrderQueue(String msg){
        logger.info("接收消息："+msg);
        Map<String, Object> resultMap = null;
        try {
            if (msg!=null&&!msg.equals("")){
                Map<String, Object> msgMap = StringUtil.mapStringToMap(msg);
                resultMap = orderDao.insertOrder(msgMap);
            }
            logger.info("队列处理完毕："+resultMap);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = ORDER_STATUS_QUEUE)
    public void updateOrderStatus(String msg){
        logger.info("接收消息："+msg);

        try {
            Map<String, Object> map = new HashMap<>();
            map = orderService.updateOrderStatus(msg);  //msg传入的订单id
            logger.info("队列处理完毕："+map);
        }catch (Exception e){
            logger.info(e.getMessage());
        }
    }
}
